#include <stdlib.h>
#include <stdio.h>

#include "../Utils/BasicTypes.h"
#include "../Utils/Utils.h"
#include "../LocalStack/LocalStack.h"

#include "../SparceMatrix/sparse_matrix.h"


#include "Matrix.h"

elem AbsSumOfCol(const elem* matrix, unsigned size, unsigned col)
{
	elem ret = 0;
	elem temp = 0;
	int i = 0;
	const unsigned end = size * size;
	
	if (matrix == NULL || size == 0)
		return -1;
	
	for (i = col; i < end;  i += size)
	{
		temp = matrix[i];
		ret += (temp > 0) ? temp : -temp;
	}

	return ret;
}

elem SumOfRow(const elem* matrix, unsigned width, unsigned row)
{
	elem ret = 0;
	int l = 0;
	const int start = width * row;
	
	for (l = start; l < start + width; l++)
		ret += matrix[l];

	return ret;
}

elem* VectorOfRowSums(const elem* matrix, elem* vector, unsigned width)
{
	int l;
	
	for (l = 0; l < width; l++)
		vector[l] = SumOfRow(matrix, width, l);
	
	return vector;
}

bool PrintMatrix(const elem* matrix, unsigned width)
{
	unsigned iter;
	const unsigned size = width * width;
	
	START();
	
	Assert(printf("%d", width) >= 0);
	for (iter = 0; iter < size; iter++)
	{
		Assert(printf(" %lf", matrix[iter]) >= 0);
	}
	
	RETURN(TRUE);
}


elem Norm (const elem* matrix, unsigned size)
{
	elem maxColSum, tempSum;
	int i;
	
	if ((matrix == NULL) || (size == 0)) //stupid assertation.!
		return -1;
	
	maxColSum = AbsSumOfCol(matrix, size, 0);
	for (i = 1; i < size; i++)
		if ((tempSum = AbsSumOfCol(matrix, size, i)) > maxColSum)
			maxColSum = tempSum;
	
	return maxColSum;
}
